version 9
#delimit;
set more off;
  quietly log;
  local logon = r(status);
  if "`logon'" == "on" {; log close; };
log using bbdw2012jop, text replace;


/*	************************************************************************	*/
/*     	File Name:	bbdw2012jop-mkdata.do						*/
/*     	Date:   	August 01, 2012							*/
/*      Author: 	Frederick J. Boehmke						*/
/*      Purpose:	Data creation file for analysis of exposure to Indian nations 	*/
/*			with and without gaming compacts in California in 1998 and 2000	*/
/*			as in Boehmke, Branton, Dillingham, and Witmer (2012, JOP). 	*/
/*			This file is included for reference only - the final data set	*/
/*			is supplied with these data but not all of the constitutive	*/
/*			pieces used to assemble it. 					*/
/*	Input File:	1998_prop5a.dta,						*/
/*			2000_initiativesa.dta,						*/
/*			newgamcas_1998.dta,						*/
/*			newgamcas_2000.dta,						*/
/*			newgamcas_2000-B.dta,						*/
/*			crime.dta							*/
/*      Output File:	bbdw2012jop-mkdata.log,						*/
/* 			bbdw2012jop.dta		 					*/
/*	************************************************************************	*/



	/* Combine the two data sets. */

use data2006-10-04gb/1998_prop5a.dta, clear;

  summarize;
  generat year = 1998;
  save bbdw2012jop, replace;

use 2000_initiativesa.dta, clear;

  summarize;
  generate year = 2000;

  append using bbdw2012jop;
  summarize;
  save bbdw2012jop, replace;

	/* Now create some variables for analysis. */

  generat Rep_Pres = Rep_Pres_1996 if year == 1998;
  replace Rep_Pres = Rep_Pres_2000 if year == 2000;

  replace med_inc_tract = med_inc_tract/10000;


  generat tribe_50 = gamingtribe_50 + nongamingtribe_50;
  generat tribe_50100 = gamingtribe_50100 + nongamingtribe_50100;

  generat tribe_100 = tribe_50 + tribe_50100;
  generat gamingtribe_100 = gamingtribe_50 + gamingtribe_50100;
  generat nongamingtribe_100 = nongamingtribe_50 + nongamingtribe_50100;

  generat nongaming50_gaming50 = gamingtribe_50*nongamingtribe_50;
  generat nongaming100_gaming100 = gamingtribe_100*nongamingtribe_100;
  generat nongaming50100_gaming50100 = gamingtribe_50100*nongamingtribe_50100;

  rename newclosest_gamingtribe close_gaming;
  rename newclosest_nongaming close_nongaming;
  rename newclosest_casino close_casino;
  rename newclosest_tribe close_tribe;
  rename closest_casinoopen close_casinoopen;

  generat close_sq = close_tribe^2;
  generat close_gaming_sq = close_gaming^2;
  generat close_nongaming_sq = close_nongaming^2;

  save bbdw2012jop, replace;



	/************************************************/
	/* Now bring in distance to all tribes data	*/
	/* with info on gaming and casino openings. 	*/
	/************************************************/

use data/newgamcas_1998.dta, clear;

  generate year = 1998;

	renpfix casino98 casino2000;  	
 
	renpfix gamcas98 	gamcas2000; 
	renpfix gamnocas98 	gamnocas2000; 
	renpfix casnogam98 	casnogam2000; 
	renpfix nogamnocas98 	nogamnocas2000; 

  append using data/newgamcas_2000.dta;

	generat pre2000 = 1;

  append using data/newgamcas_2000-B.dta;

	replace year = 2000 if year == .;
	replace pre2000 = 0 if pre2000 == .;

  generate double temp = real(fips);

	drop fips;
	rename temp fips;

  	drop if fips == .;

	drop casino2000_distance* gaming2000_distance*; 

	renpfix gamcas2000_distance 	gamcas;
	renpfix gamnocas2000_distance 	gamnoc;
	renpfix casnogam2000_distance 	nogcas;
	renpfix nogamnocas2000_distance nognoc;


  foreach var in gamcas gamnoc nogcas nognoc {;

	foreach dist of numlist 5 10 25 50 75 100 150 200 {;

	  quietly generat `var'_tribe_`dist' = 0;

	  };

	};


  foreach var in gamcas gamnoc nogcas nognoc {;

	foreach dist of numlist 5 10 25 50 75 100 150 200 {;

	  forvalues num=1/98 {;

		capture confirm numeric variable `var'`num';

		if _rc == 0 {;

		  quietly replace `var'_tribe_`dist' = `var'_tribe_`dist' + 1 if `var'`num' <= `dist' & !missing(`var'`num');  

		  };

		else {; };

		};

	  };

	};


  foreach dist of numlist 5 10 25 50 75 100 150 200 {;

	quietly generat casino_tribe_`dist' = gamcas_tribe_`dist' + nogcas_tribe_`dist';
	quietly generat gaming_tribe_`dist' = gamcas_tribe_`dist' + gamnoc_tribe_`dist';

	quietly generat noncas_tribe_`dist' = gamnoc_tribe_`dist' + nognoc_tribe_`dist';
	quietly generat nongam_tribe_`dist' = nogcas_tribe_`dist' + nognoc_tribe_`dist';

	quietly generat tribe_`dist' = gaming_tribe_`dist' + nongam_tribe_`dist';

	};


  foreach var in gamcas gamnoc nogcas nognoc {;

	foreach value in 2500 1000 0500 0250 0125 0075 {;

  	  quietly generat `var'_exp_`value' = 0;

	  };

	};


  foreach var in gamcas gamnoc nogcas nognoc {;

  	forvalues num=1/98 {;
	
	  foreach value in 2500 1000 0500 0250 0125 0075 {;

		capture confirm numeric variable `var'`num';

		if _rc == 0 {;

		  quietly replace `var'_exp_`value' = `var'_exp_`value' + exp(-0.`value'*`var'`num') if !missing(`var'`num');

		  };

		else {; };
		
		};

	  };

	};


  foreach value in 2500 1000 0500 0250 0125 0075 {;

	quietly generat casino_exp_`value' = gamcas_exp_`value' + nogcas_exp_`value';
	quietly generat gaming_exp_`value' = gamcas_exp_`value' + gamnoc_exp_`value';

	quietly generat noncas_exp_`value' = gamnoc_exp_`value' + nognoc_exp_`value';
	quietly generat nongam_exp_`value' = nogcas_exp_`value' + nognoc_exp_`value';

	quietly generat exp_`value' = gaming_exp_`value' + nongam_exp_`value';

	};


  keep fips year *exp* *tribe* pre2000;


	/****************************************/
	/* Now merge with county and fips data.	*/
	/****************************************/


  joinby fips year using bbdw2012jop.dta, _merge(_merge_fips_cas) unmatched(both);

	format fips %10.0f;

  merge m:1 county year using data/crime.dta, generat(_merge_crime) keep(match master);
	
label data "Initiative voting and tribal proximity (Boehmke, Branton & Witmer 2006)";

  label variable Prop5_yes 		"Tract Vote for Prop 5 (%)";
  label variable Prop1_yes 		"Tract Vote for Prop 1A (%)";
  label variable Prop29_yes 		"Tract Vote for Prop 29 (%)";
  label variable Rep_Pres 		"Republican Presidential Vote";
  label variable urban_tract 		"Percent Urban";
  label variable per_urban_tract 	"Percent Urban";
  label variable inc_tract_1000 	"Income";
  label variable med_inc_tract 		"Income (10,000s)";
  label variable med_age_tract 		"Median Age";
  label variable age_age_tract 		"Median Age - Squared";
  label variable indian_tract 		"Percent Native American";
  label variable white_tract 		"Percent White";
  label variable black_tract 		"Percent Black";
  label variable high_school_tract 	"Percent High School";
  label variable college_tract 		"Percent College";
  label variable close_tribe 		"Miles to Closest Tribe";
  label variable close_gaming		"Miles to Closest Gaming Tribe";
  label variable close_sq 		"Miles to Closest Tribe - Squared";
  label variable close_gaming_sq	"Miles to Closest Gaming Tribe - Squared";
  label variable tribe_50	 	"Tribes Within 50 Miles";
  label variable gamingtribe_50 	"Gaming Tribes Within 50 Miles";
  label variable nongamingtribe_50  	"Nongaming Tribes Within 50 Miles";
  label variable tribe_100 		"Tribes Within 100 Miles";
  label variable gamingtribe_100 	"Gaming Tribes Within 100 Miles";
  label variable nongamingtribe_100 	"Nongaming Tribes Within 100 Miles";
  label variable tribe_50100 		"Tribes Within 50-100 Miles";
  label variable gamingtribe_50100	"Gaming Tribes Within 50-100 Miles";
  label variable nongamingtribe_50100	"Nongaming Tribes Within 50-100 Miles";
  label variable nongaming50_gaming50 	"Interaction of Non-gaming and Gaming Tribes Within 50 Miles";

  foreach var in gamcas gamnoc nogcas nognoc gaming nongam casino noncas {;

  	foreach value in  2500 1000 0500 0250 0125 0075 {;

	  label variable `var'_exp_`value' 	"`var' Exposure (d=-0.`value')";

	  };

  	};

  compress;

  save bbdw2012jop, replace;

log close;
clear;
exit, STATA;
